const modules = import.meta.glob('@/views/**/index.vue')

export function convertMenusToRoutes(menus) {
  const children = []

  menus.forEach(menu => {
    const items = menu.children?.length ? menu.children : [menu]

    items.forEach(item => {
      if (!item.path) return

      const viewPath = `/src/views${item.path.replace('/app', '')}/index.vue`
      const component = modules[viewPath]

      if (!component) {
        console.warn('❌ 路由路径不存在组件：', viewPath)
        return
      }

      children.push({
        path: item.path.replace('/app/', ''),
        name: item.name,
        component
      })
    })
  })

  return [
    {
      path: '/app',
      component: () => import('@/views/layout/index.vue'),
      redirect: '/app/index',
      children
    }
  ]
}